/*
页面加载完毕 发起请求获取七牛token，通过前台传到七牛
*/
function getTokenMessage() {
	var url="";
	var qiniupercent="";
	var uploader="btn-uploader";
	var pickfiles="pickfiles";
	$.ajax({
		url: token_url,
		type: 'POST',
		data: {},
		cache: false,
		contentType: false,    //不可缺
		processData: false,    //不可缺
		dataType : 'json',
		success: function (data) {
			var obj = data;
			uploaderReady(obj.uptoken,url,qiniupercent,uploader,pickfiles);
		}
    });
}


//链接类型
var clickControl=0;
$(".popSite").on("click","[name='linkType']",function(){
	var selecttxt = $(".popSite [name='linkType'] option:selected").val();
	if(clickControl%2 ==1){
		getPage(selecttxt);
	}
	clickControl++;
});


$(".popSite").on("blur","[name='linkType']",function(){	
	clickControl=0;
});

//提交数据
$('.btn_add_pop').click(function() {
    var _data = $("#form_role_new").serializeObject();
    var imgUrl= $(' [name="img_qiniu_url"]')[0].src; 
    if(imgUrl.indexOf(window.location.pathname)>-1){
   	    imgUrl="";
    }
    var linkType=$('select[name="linkType"]').val();
    var linkId=$('input[name="linkId"]').val();
    if(!imgUrl){
        layer.msg("请上传广告图片");
        return ;
    }else if(!linkType){
        layer.msg("请选择链接类型");
        return;
    }else if(linkType ==  '7'){
        //console.log(linkId);
        if (!linkId) {
            layer.msg("请选择弹框信息");
            return;
        }            		
    }else {
        linkId="";
    }  

    var id=_data.id;
    var param={"imgUrl":imgUrl,"linkId":linkId,"linkType":linkType,"status":_data.status,"id":id};
	var _type = id == null||id == '' ? "put" : "post";
	$.ajax({
		url : add_url,
		type : _type,
		dataType : 'json',
		contentType:"application/json; charset=utf-8",
		data : JSON.stringify(param),
	}).done(function(data) {
		if (data.code === '1') {
            layer.msg('保存成功');
			window.location.reload();
		} else if (data.code === '2') {
			layer.msg.html(data.message); // 返回错误信息
		}
	});
     
});
		
$('#table_role .select-all').click(function() {
	if ($(this).is(':checked')){
	 $("#table_role :checkbox").prop("checked", true);
	 $("#table_role tbody tr").addClass("selected");
	} else {
	 $("#table_role :checkbox").prop("checked", false);
	 $("#table_role tbody tr").removeClass("selected");
	}
});

$("#table_role").on('click', '.checkchild', function(event) {
	$(this).parents("tr").toggleClass("selected");
});


$('.btn_select').on('click', function() { 
	var data=$('#table_role').DataTable().rows('.selected').data();
	var length=data.length;
	if(length>3){
		layer.msg.html("最多只能选择三条数据");
		return ;
	}
	var ids="";
	for(var i=0;i<length;i++){
		if(i==0){
			ids=data[i].id;
		}else{
			ids+=","+data[i].id;
		}
		
	}
	$('#linkId').val(ids);
	$('#select-coupon').modal('hide');
}); 
	
$(function() {
	getTokenMessage();

	$('[name="switch"]').bootstrapSwitch({  
        onText:"是",  
        offText:"否",
        size:"small",  
        onSwitchChange:function(event,state){  
            if(state==true){  
            	$('[name="status"]').val("1");  
            }else{  
            	$('[name="status"]').val("0");  
            }  
        }  
    })  
  
});